﻿@model List<SnongPhoneShop.Models.Cart>
@{
    ViewBag.Title = "Cập nhật giỏ hàng";
    Layout = "~/Views/Shared/_LayoutAccMetroUI.cshtml";
    ViewBag.CurrPage = "Giỏ hàng";
    long total_price = 0;
}
@section selected_link{
    <style>
        .cart-info {
            background:#808080;
        }
    </style>    
}
<table border="1" class="cart-table fixed-header">
    <tr>
        <th class="update-image-product">Ảnh</th>
        <th class="update-name-product">Tên sản phẩm</th>
        <th class="update-price-product">Đơn giá (VNĐ)</th>
        <th class="update-number-product">Số lượng</th>
        <th class="update-sum-price-product">Thành tiền (VNĐ)</th>
        <th class="update-del-product">Xóa</th>
    </tr>
</table>
@using (Html.BeginForm()) { 
    <div class="hidden-scrollbar">
        <div class="body-table scroll-enable">
            <table border="1" class="cart-table">
                <script type="text/javascript">
                    function Sum_Price(i) {
                        var price = $('#price-' + i).data('price');
                        var num = $('#num-' + i).val();
                        $('#sum-price-' + i).text(price * num);
                        return price * num;
                    }

                    function Total_Price(n) {
                        var T = 0;
                        for (var i = 0; i < n; i++) {
                            T += Sum_Price(i);
                        }
                        $('#num-total-price').text(T);
                    }

                    function Num_Valid(i) {
                        var num = $('#num-' + i).val();
                        if (num < 1) {
                            alert('Số lượng phải lớn hơn 0');
                            $('#num-' + i).val(1);
                        }
                        if (num > 5) {
                            alert('Số lượng phải nhỏ hơn 5');
                            $('#num-' + i).val(5);
                        }
                    }
                </script>
                @for (int i = 0; i < Model.Count; i++)
                {
                    long sum_price = Model[i].SoLuong * Model[i].SanPham.DonGia;
                    <tr>
                        <td class="update-image-product"><img src="@Model[i].SanPham.AnhDaiDien" /></td>
                        <td class="update-name-product">@Model[i].SanPham.TenSanPham</td>
                        <td class="update-price-product" id="price-@i" data-price="@Model[i].SanPham.DonGia">@Model[i].SanPham.DonGia.ToString("n0")</td>
                        <td class="update-number-product" onchange=" Num_Valid(@i); Sum_Price(@i); Total_Price(@Model.Count);">@Html.TextBoxFor(m => Model[i].SoLuong, new { id = "num-" + i })</td>
                        <td class="update-sum-price-product" id="sum-price-@i">@sum_price.ToString("n0")</td>
                        <td class="update-del-product"><a href="@Url.Action("Xoa_SP_GH", new { ID = Model[i].MaSanPham })"><img src="/Images/ImgPage/trash.png" /></a></td>
                        @Html.HiddenFor(m => Model[i].MaSanPham)
                    </tr>
                    total_price += sum_price;
                }
            </table>
        </div>
    </div>
    <table border="1" class="cart-table fixed-footer">
        <tr>
            <th colspan="5" class="total-price">Tổng tiền: <b id="num-total-price">@total_price.ToString("n0")</b> (VNĐ)</th>
        </tr>
    </table>
    <table border="1" class="cart-table">
        <tr>
            <td colspan="6" class="action-table">
                <input type="submit" value="Cập nhật" />
                <a href="@Url.Action("Gio_Hang", "Tai_Khoan")"><span>Trở lại</span></a>
            </td>
        </tr>
    </table>
}

